package com.zyn.rsgl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyn.rsgl.mapper.HolidayMapper;
import com.zyn.rsgl.mapper.ScheduleDetailMapper;
import com.zyn.rsgl.mapper.ScheduleMapper;
import com.zyn.rsgl.pojo.Holiday;
import com.zyn.rsgl.pojo.Schedule;
import com.zyn.rsgl.pojo.ScheduleDetail;
import com.zyn.rsgl.service.ScheduleService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {

    @Resource
    private ScheduleDetailMapper scheduleDetailMapper;

    @Override
    public R listSchedulesPage(Integer pageNum, Integer pageSize, String name, String description) {
        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>();
        if (name != null) {
            queryWrapper.like("name", name);
        }
        if (description != null) {
            queryWrapper.like("description", description);
        }
        queryWrapper.orderByDesc("id");

        Page<Schedule> page = PageHelper.startPage(pageNum, pageSize);
        List<Schedule> list = this.list(queryWrapper);

        if (list != null) {
            return R.ok(list, page.getTotal());
        }
        return R.fail("查询失败");
    }

    @Override
    public R listDetailsPage(String scheduleId, Integer pageNum, Integer pageSize, String dayOfWeek, String startTime, String endTime) {
        QueryWrapper<ScheduleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schedule_id", scheduleId);

        if (dayOfWeek != null && !dayOfWeek.isEmpty()) {
            queryWrapper.eq("day_of_week", dayOfWeek);
        }
        if (startTime != null && !startTime.isEmpty()) {
            queryWrapper.ge("start_time", startTime);
        }
        if (endTime != null && !endTime.isEmpty()) {
            queryWrapper.le("end_time", endTime);
        }
        queryWrapper.orderByDesc("day_of_week");

        Page<ScheduleDetail> page = PageHelper.startPage(pageNum, pageSize);
        List<ScheduleDetail> list = scheduleDetailMapper.selectList(queryWrapper);

        if (list != null) {
            return R.ok(list, page.getTotal());
        }
        return R.fail("查询失败");
    }
    @Override
    public boolean detailExists(ScheduleDetail detail) {
        QueryWrapper<ScheduleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schedule_id", detail.getScheduleId())
                .eq("day_of_week", detail.getDayOfWeek());
        return scheduleDetailMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public ScheduleDetail getScheduleDetailByDate(String userId, LocalDateTime date) {
        // 获取日期对应的星期几
        LocalDate localDate = date.toLocalDate(); // 提取日期部分
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        ScheduleDetail detail = scheduleDetailMapper.getScheduleDetailByDayOfWeek(userId, getChineseDayOfWeek(dayOfWeek));
        return detail;
    }
    public static String getChineseDayOfWeek(DayOfWeek dayOfWeek) {
        switch (dayOfWeek) {
            case MONDAY:
                return "星期一";
            case TUESDAY:
                return "星期二";
            case WEDNESDAY:
                return "星期三";
            case THURSDAY:
                return "星期四";
            case FRIDAY:
                return "星期五";
            case SATURDAY:
                return "星期六";
            case SUNDAY:
                return "星期日";
            default:
                return "未知";
        }
    }
}